(function() {
	var util = require('common:widget/ui/libs/util.js').util,
        pageUtil =  {
		head : null,
		init : function() {
			var me = this;
			//调整页面大小
			var windowHeight = $(window).height();
			$('.section').height(windowHeight);
			if(windowHeight < 840 + 80 +135) {
				$('.register_form').css('top',100);
				$('#footer').css('top',630 + 80);
				$('.section').height('1055');
			} else {
				var height = $(window).height()/2-370;
				$('.register_form').css('top',height);	
			}
			
			var head = require('home:widget/ui/whitehead/whitehead.js');
			me.head = new head($('.section') , 'white');
			this.eventCenter();
		},
		eventCenter : function() {
			var me = this;
			$('.vc_button').bind('click',{'me':me}, me.sendVC);
			$('.input_text').click(function(event) {
				$(event.target).find('input').focus();
			});
			$('.form_el input').change(function() {
				me.checkForm(event.target);
			});
			$('#register_button').click(function() {
				var input_list = $('.form_el input');
				for(var i=0; i < input_list.length; i++) {
					var one_el = input_list[i];
					var status = me.checkForm(one_el);
					if(!status) {
						break;
					}
				}
				me.submitForm();
			});

            var urlPhone = util.getUrlParas();
            if(urlPhone['phone']){
                $('#mobile').val(urlPhone['phone']);
                $('.vc_button').trigger('click');
            }
		},
		submitForm: function() {
			var me = this;
			var mobile = $('#mobile').val();
			var vc = $('#vc').val();
			var password = $('#password').val();
			var recommender = $('#recommender').val();
			$.post('/account/registeruser',{'user_name':mobile, 'code':vc, 'passwd': password,'recommender':recommender} ,function(data) {
				if(data && data.error_no == 0) {
					alert('注册成功');
					location.href='/index';
				} else {
					if(data.error_no == 10101 || data.error_no == 10102) {
						me.setMessage($('#mobile'),data.error_message);
					} else if(data.error_no == 10203 || data.error_no == 10204) {
						me.setMessage($('#vc'),data.error_message);
					} else if (data.error_no == 10210){
						me.setMessage($('#recommender'),data.error_message);
					}else {
						alert(data.error_message);
					}

				}
			},'json');
		},
		sendVC : function(event) {
			//发送验证码：
			var mobile = $('#mobile').val();
			if(!mobile) {
				event.data.me.setMessage($('#mobile'), '请输入手机号。');
				return;
			}
			var type = 0;
			if(event.data && event.data.type) {
				type = event.data.type;
			}
			$('.get_vc').text(60);
			$('.get_vc').unbind('click');
			$.post('/account/sendidentitycode',{'mobile':mobile,'type':type},function(data) {
				event.data.me.clearMessage($('#vc'));
				if(data && data.error_no == 0) {
					var setTime = setInterval(function(){
						var time = $('.get_vc').text();
							if(parseInt(time) <= 1) {
								$('.get_vc').text('获取');
								$('.get_vc').removeClass('disabled');
								$('.get_vc').bind('click',{'me':event.data.me},event.data.me.sendVC);
                                setTime && clearInterval(setTime);
                                setTime = 0;
								event.data.me.setMessage($('#vc'), '如果1分钟内没有收到校验短信，您可以通过<a id="get_voice_vc">语音方式</a>获取验证码，此服务免费。','info_msg');
								$('#get_voice_vc').css('cursor','pointer').css('color','#FFF').css('text-decoration','underline').css('padding','0px 10px');
								$('#get_voice_vc').bind('click',{'me':event.data.me, 'type':1},event.data.me.sendVC);
							} else {
								$('.get_vc').text(parseInt(time) -1);
							}
					},1000);
				} else {
					event.data.me.setMessage($('#vc'),data.error_message);
				}
			},'json');
		},
		checkForm: function(html) {
			var me = this;
			var id = $(html).attr('id');
			var jId = '#' + id;
			me.clearMessage($(jId));
			if(id == 'mobile') {
				if($(jId).val().length < 11){
					me.setMessage($(jId), '请输入11位手机号。');
					return false;
				}
			} else if(id == 'vc') {
				if($(jId).val().length < 6) {
					me.setMessage($(jId), '请输入6位验证码。');
					return false;
				}
			} else if(id == 'password' || id=='repassword') {
				if($(jId).val().length < 6 || $(jId).val().length > 16) {
					me.setMessage($(jId),'密码长度需要6~16位。');
					return false;
				}
				if(id == 'repassword' && $('#password').val()!= $('#repassword').val())  {
					me.setMessage($(jId),'2次密码不一致。');
					return false;
				}
			} else if(id == 'recommender') {
				if($(jId).val().length >0 && $(jId).val().length < 11){
					me.setMessage($(jId), '请输入11位手机号。');
					return false;
				}
				if($(jId).val() == $('#mobile').val()) {
					me.setMessage($(jId),'推荐人不可与本人号码一致。');
					return false;
				}
			}
			if(!$('#agree_info').is(':checked')) {
				alert('请确认阅读《真融宝用户服务协议》。');
				return false;
			}
			return true;
		},
		clearMessage:function($container) {
			$container.parent().parent().find('.info').text('');
		},
		setMessage:function($container,message,type) {
			$('.form_el .info').text('');
			type = type || 'error_msg';
			$infoContainer = $container.parent().parent().find('.info');
			if(message.length > 15) {
				$infoContainer.css('margin-top','0px');
			} else{
				$infoContainer.css('margin-top','20px');
			}
			$infoContainer.html(message);
			$infoContainer.removeClass('error_msg');
			$infoContainer.removeClass('info_msg');
			$infoContainer.addClass(type);
		}
	}
	pageUtil.init();
})();


